#-*-coding:utf-8-*-


from PyQt4 import QtCore, QtGui
import sys, os, time
from fonk import VERSION, byte, mesaj
from noplatform import *
from tempfile import gettempdir

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(400, 150)
        self.setMinimumSize(400,150)
        self.setMaximumSize(400,150)
        self.setWindowTitle(u"Odun Updater")
        self.setWindowIcon(QtGui.QIcon(os.path.join("images", "odun.png")))
        self.cw = QtGui.QWidget(self)
        self.setCentralWidget(self.cw)
        self.mbar = QtGui.QMenuBar(self)
        self.mDosya = QtGui.QMenu(self.mbar)
        self.mYardim = QtGui.QMenu(self.mbar)
        self.setMenuBar(self.mbar)
        self.aHakkinda = QtGui.QAction(self)
        self.aKullanimKlavuzu = QtGui.QAction(self)
        self.aQtHakkinda = QtGui.QAction(self)
        self.aQtHakkinda.setText(u"Qt Hakkında")
        self.aKapat = QtGui.QAction(self)
        self.aKapat.setText("Kapat")
        self.mDosya.addAction(self.aKapat)
        self.mYardim.addAction(self.aKullanimKlavuzu)
        self.mYardim.addSeparator()
        self.mYardim.addAction(self.aHakkinda)
        self.mYardim.addAction(self.aQtHakkinda)
        self.mbar.addAction(self.mDosya.menuAction())
        self.mbar.addAction(self.mYardim.menuAction())
        self.mDosya.setTitle(u"Dosya")
        self.mYardim.setTitle(u"Yardım")
        self.aHakkinda.setText(u"Odun Hakkında")
        self.aKullanimKlavuzu.setText(u"Kullanım Klavuzu")
        self.kutucuk = QtGui.QCheckBox(self)

        QtCore.QObject.connect(self.aKapat, QtCore.SIGNAL("triggered(bool)"), self.close)
        QtCore.QObject.connect(self.aHakkinda, QtCore.SIGNAL("triggered(bool)"), self.hakkinda)
        QtCore.QObject.connect(self.aKullanimKlavuzu, QtCore.SIGNAL("triggered(bool)"), self.klavuz)
        QtCore.QObject.connect(self.aQtHakkinda, QtCore.SIGNAL("triggered(bool)"), QtGui.QApplication.aboutQt)

        
        self.gLayout = QtGui.QGridLayout(self.cw)
        self.gLayout.addWidget(self.kutucuk, 2, 2, 1, 1,)
        self.sbar = QtGui.QLabel(self.cw)
        self.sbar.setText(u"")
        self.gLayout.addWidget(self.sbar, 3,0,1,4)
        self.label = QtGui.QLabel(self.cw)
        self.label2 = QtGui.QLabel(self.cw)
        self.gLayout.addWidget(self.label2, 0,2,1,1)
        self.label2.hide()
        self.gLayout.addWidget(self.label, 0,2,1,1)
        self.cHayir = QtGui.QRadioButton(self.cw)
        self.gLayout.addWidget(self.cHayir, 2,0,1,1)
        self.cEvet = QtGui.QRadioButton(self.cw)
        self.gLayout.addWidget(self.cEvet, 1,0,1,1)
        self.cEvet.setChecked(True)
        self.kaydet = QtGui.QPushButton(self.cw)
        self.gLayout.addWidget(self.kaydet, 1,2,1,1)
        self.soru1 = QtGui.QLabel(self.cw)
        self.gLayout.addWidget(self.soru1, 0,0,1,1)
        self.updated = QtGui.QPushButton(self.cw)
        self.updated.hide()
        self.gLayout.addWidget(self.updated, 1,2,1,1)
        self.moviel = QtGui.QLabel(self.cw)
        self.moviel2 = QtGui.QLabel(self.cw)
        self.loading = QtGui.QMovie(os.path.join("images", "loading.gif"))
        self.loading2 = QtGui.QMovie(os.path.join("images", "loading.gif"))
        self.moviel.setMovie(self.loading)
        self.moviel2.setMovie(self.loading2)
        self.gLayout.addWidget(self.moviel, 1,3,1,1)
        self.gLayout.addWidget(self.moviel2, 1,3,1,1)

        self.label.setText(u"Mevcut Paketleri Al:")
        self.label2.setText(u"Güncellemeleri Al:")
        self.kaydet.setText(u"Paketleri Al")
        self.cHayir.setText(u"Hayır")
        self.cEvet.setText(u"Evet")
        self.soru1.setText(u"İnternetiniz Var mı?")
        self.kutucuk.setText("Delta")
        self.kutucuk.setChecked(True)
        self.label.hide()
        self.moviel2.hide()
        self.kaydet.hide()
        self.updated.show()
        self.moviel.show()
        self.label2.show()
        self.updated.setText(u"Al")

        QtCore.QObject.connect(self.kaydet, QtCore.SIGNAL("clicked(bool)"), self.xmlKaydet)
        QtCore.QObject.connect(self.cHayir, QtCore.SIGNAL("clicked(bool)"), self.checkbox)
        QtCore.QObject.connect(self.cEvet, QtCore.SIGNAL("clicked(bool)"), self.checkbox)
        QtCore.QObject.connect(self.updated, QtCore.SIGNAL("clicked(bool)"), self.getUpdatedList)

    def xmlKaydet2(self):
        self.sbar.setText(u"Yüklü paketler alındı...")
        time.sleep(2)
        self.loading2.stop()
        repo = open(os.path.join(gettempdir(), "installed-package.xml"), "r")
        fd = QtGui.QFileDialog.getSaveFileName(self, "Kaydet", QtCore.QDir.homePath(), u"XML Dosyası (*.xml)")
        if fd[-3:] == "xml":
            xmlyaz = open(fd,"w")
            xmlyaz.write(repo.read())
            xmlyaz.close()
        else:
            xmlyaz = open(fd+".xml","w")
            xmlyaz.write(repo.read())
            xmlyaz.close()
        self.sbar.setText(u"İşlem tamamlandı.")
        # paketleri al butonu etkinleştirildi
        self.kaydet.setEnabled(True)
        # ikinci radio button tekrar etkinleştirildi
        self.cEvet.setEnabled(True)

    def xmlKaydet(self):
        # paketleri al butonu etkisizleştirildi
        self.kaydet.setEnabled(False)
        # ikinci radio button etkisiz hale getirildi
        self.cEvet.setEnabled(False)
        self.loading2.start()
        self.sbar.setText(u"Yüklü paketler alınıyor...")
        self.thread = arac.OdunThread(pkg.getInstalledPackages, [])
        self.thread.start()
        self.thread.wait(1)
        QtCore.QObject.connect(self.thread, QtCore.SIGNAL("finished()"), self.xmlKaydet2)

    def getUpdatedList2(self):
        self.sbar.setText(u"Güncellemeler alındı.")
        time.sleep(2)
        self.loading.stop()
        repo = open(os.path.join(gettempdir(), "updates.pdx"),"r")
        fd = QtGui.QFileDialog.getSaveFileName(self, "Kaydet", QtCore.QDir.homePath(), u"PDX Dosyası (*.pdx)")
        if fd[-3:] == "pdx":
            xmlyaz = open(fd,"w")
            xmlyaz.write(repo.read())
            xmlyaz.close()
        else:
            xmlyaz = open(fd+".pdx","w")
            xmlyaz.write(repo.read())
            xmlyaz.close()
        self.sbar.setText(u"İşlem Tamamlandı.")
        self.updated.setEnabled(True)
        self.kutucuk.setEnabled(True)
        # diger radio button etkinleştirildi
        self.cHayir.setEnabled(True)

    def getUpdatedList(self):
        if self.kutucuk.isChecked():
			self.delta = True
        else:
			self.delta = False
        self.updated.setEnabled(False)
        self.kutucuk.setEnabled(False)
        # diger radio button etkisizleştirildi
        self.cHayir.setEnabled(False)
        self.loading.start()
        #pkg.update_repo()
        self.thread3 = arac.OdunThread(pkg.update_repo, ())
        self.thread3.start()
        self.sbar.setText(u"Güncellemeler alınıyor...")
        QtCore.QObject.connect(self.thread3, QtCore.SIGNAL("finished()"), self.aragaz)

    def aragaz(self):
        self.thread2 = arac.OdunThread(pkg.writeUpdatedPackages, [self.delta])
        self.thread2.start()
        self.thread2.wait(1)
        QtCore.QObject.connect(self.thread2, QtCore.SIGNAL("finished()"), self.getUpdatedList2)

    def checkbox(self):
        if self.cEvet.isChecked():
            self.label.hide()
            self.moviel2.hide()
            self.kaydet.hide()
            self.updated.show()
            self.moviel.show()
            self.label2.show()
            self.kutucuk.show()

        if self.cHayir.isChecked():
            self.updated.hide()
            self.moviel.hide()
            self.label2.hide()
            self.label.show()
            self.moviel2.show()
            self.kaydet.show()
            self.kutucuk.hide()

    def hakkinda(self):
        mesajk = QtGui.QMessageBox.about(self.cw, u"Odun Hakkında", mesaj)

    def klavuz(self):
        pass


if __name__ == "__main__":
    if isPardus():
        import arac
        from pardus import Package
        pkg = Package()
        app = QtGui.QApplication(sys.argv)
        ui = MainWindow()
        ui.show()
        sys.exit(app.exec_())
    else:
        print "Pardus dışında çalışmaz!"
        sys.exit()
